package com.finger.hospservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.finger.hospservice.repository.HospitalRepository;
import com.finger.hospservice.service.DictService;
import com.finger.hospservice.service.HospitalService;
import com.finger.modelservice.model.hosp.Hospital;
import com.finger.modelservice.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictService dictService;

    @Override
    public void saveHospital(Map<String, Object> map) {

        String jsonString = JSONObject.toJSONString(map);

        Hospital hospital = JSON.parseObject(jsonString, Hospital.class);

        String hoscode = hospital.getHoscode();

        Hospital existHospital = hospitalRepository.getHospitalByHoscode(hoscode);

        if (existHospital != null) {
            hospital.setId(existHospital.getId());
            hospital.setStatus(existHospital.getStatus());
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(existHospital.getCreateTime());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            // 0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {

        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);

        return hospital;
    }

    @Override
    public Page<Hospital> findHospital(int page, int size, HospitalQueryVo hospitalQueryVo) {

        Pageable pageable = PageRequest.of(page-1,size);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();

        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Example<Hospital> example = Example.of(hospital,matcher);

        Page<Hospital> all = hospitalRepository.findAll(example, pageable);

        all.getContent().stream().forEach(item ->{
           this.setName(item);
        });

        return all;
    }

    @Override
    public void updateStatus(String id, Integer status) {

        Hospital hospital = hospitalRepository.findById(id).get();

        if(hospital!=null){
            hospital.setStatus(status);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Map<String, Object> getById(String id) {

        Hospital hospital = hospitalRepository.findById(id).get();

        // 设置医院等级和地址
        this.setName(hospital);

        Map<String,Object> map = new HashMap();

        map.put("bookingRule",hospital.getBookingRule());

        hospital.setBookingRule(null);
        map.put("hospital",hospital);

        return map;
    }

    @Override
    public String getHosName(String hoscode) {

        Hospital hospital = new Hospital();
        hospital.setHoscode(hoscode);
        Example<Hospital> of = Example.of(hospital);

        Hospital result = hospitalRepository.findOne(of).get();

        if(result!=null){
            return result.getHosname();
        }

        return null;
    }

    @Override
    public List<Hospital> findHospitalLikeHosName(String hosname) {

        List<Hospital> hospitalList = hospitalRepository.findHospitalByHosnameLike(hosname);

        return hospitalList;
    }

    @Override
    public Map<String, Object> findHospitalByHoscode(String hoscode) {

        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);

        Map<String,Object> resultMap = new HashMap();
        if(hospital!=null){

            this.setName(hospital);
            resultMap.put("hospital",hospital);
            resultMap.put("bookingRule",hospital.getBookingRule());
            hospital.setBookingRule(null);
        }

        return resultMap;
    }

    private Hospital setName(Hospital item){
        Map<String, Object> param = item.getParam();
        String provinceCode = item.getProvinceCode();
        String cityCode = item.getCityCode();
        String districtCode = item.getDistrictCode();

        String hostypeStr = dictService.getName("Hostype", item.getHostype());

        String provinceStr = dictService.getName("", provinceCode);
        String cityStr = dictService.getName("", cityCode);
        String districtStr = dictService.getName("", districtCode);

        param.put("hostypeStr",hostypeStr);
        param.put("fillAddress",provinceStr+cityStr+districtStr);

        return item;
    }
}
